package com.grechur.wanandroidcompose.viewmodel

import android.app.Application
import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.AndroidViewModel
import androidx.navigation.NavHostController
import com.grechur.wanandroidcompose.WanApplication
import com.grechur.wanandroidcompose.bean.BaseData
import com.grechur.wanandroidcompose.http.service.LoginService
import com.grechur.wanandroidcompose.http.NetworkRequest

class LoginViewModel(application: Application) : AndroidViewModel(application) {
    var isRegist by mutableStateOf(false)
        private set

    var registError by mutableStateOf("")
        private set

    var isLoginSuccess by mutableStateOf(false)
        private set

    var loginInfo by mutableStateOf(Result.success(BaseData<Any>(Any(),-1, "")))
        private set

    var globalNavController: NavHostController? = null

    suspend fun login(name: String, pwd: String) {
        val newData = try {
            val service = NetworkRequest.getService(getApplication(), LoginService::class.java)
            val login = service.login(name, pwd)
            val result = login.takeIf { it != null }?.let { baseData ->
                if (baseData.errorCode == 0) {
                    WanApplication.getLoginCookie(true)
                    isLoginSuccess = true
                    Log.e("zhouzhu","login method success isLogin:$isLoginSuccess")
                    globalNavController?.popBackStack()
                    Result.success(baseData)
                } else {
                    Result.failure(IllegalArgumentException("error code ${baseData.errorCode} msg ${baseData.errorMsg}"))
                }
            } ?: Result.failure(IllegalArgumentException("data is null"))
            result
        } catch (e: Exception) {
            Result.failure(e)
        }
        loginInfo = newData
    }

    suspend fun logout() {
        val logout = try {
            val service = NetworkRequest.getService(getApplication(), LoginService::class.java)
            val login = service.logout()
            login.takeIf { it != null }?.let { baseData ->
                if(baseData.errorCode == 0){
                    WanApplication.clearLoginInfo()
                }
                baseData.errorCode == 0
            } ?: false
        } catch (e: Exception) {
            false
        }
        isLoginSuccess = !logout
    }

    suspend fun register(name: String, pwd: String, rePwd: String) {
        val newError = try {
            val service = NetworkRequest.getService(getApplication(), LoginService::class.java)
            val logout = service.register(name, pwd, rePwd)
            val error = logout.takeIf { it != null }?.let { baseData ->
                isRegist = baseData.errorCode == 0
                if (baseData.errorCode != 0) {
                    baseData.errorMsg ?: "注册失败"
                } else {
                    baseData.errorMsg ?: "注册成功"
                }
            } ?: "注册失败"
            error
        } catch (e: Exception) {
            isRegist = false
            e.message?:"注册失败"
        }
        registError = newError
    }

    fun checkLogin() {
        if (WanApplication.isLogin) {
            isLoginSuccess = true
        }
    }

}